package com.example.aaa_viewpager;

import java.util.ArrayList;
import java.util.List;

import com.example.aaa_viewpager.entity.UserCustom;
import com.example.aaa_viewpager.service.UserCustomDao;
import com.example.bb_serch.R;
import com.example.bb_serch.R.id;
import com.example.bb_serch.R.layout;
import com.example.bb_serch.R.menu;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.text.style.ClickableSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class BB_notes extends Activity {

	// 從fragment3傳過來的資訊
	private String favorite_name;

	// /data
	private UserCustomDao userCustomDao;

	private UserCustom userCustom;

	// UI
	private TextView title, subTitle;
	private EditText inputText;
	private ImageView note_img;

	// 輸入文字
	private String items = null;

	// 讀取存進 NOTES 的資料
	private UserCustom showNotes;
	private UserCustom address;
	private UserCustom pic;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bb_notes);
		//限制應用程式只能直立，禁止橫向
		setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
		initView();
		// initHandler();
	}

	// private void initHandler()
	// {
	//
	// items = inputText.getText().toString();
	// //儲存資料到 custom.db
	// userCustom = new UserCustom(items);
	// userCustomDao.update(items, favorite_name);
	//
	//
	// }

	// 接收從fragment3傳過來的資料
	private void getData() {
		Bundle bundle = this.getIntent().getExtras();
		favorite_name = bundle.getString("BB");

	}

	private void initView() {

		title = (TextView) findViewById(R.id.note_title);
		subTitle = (TextView) findViewById(R.id.note_bb_address);
		note_img = (ImageView) findViewById(R.id.bb_img);

		// 設置EditText的顯示方式為多行输入
		inputText = (EditText) findViewById(R.id.inputText);
		inputText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
		inputText.setSingleLine(false);
		
		userCustomDao = new UserCustomDao(this);
		// noteData = userCustomDao.getAll();
		getData();
		refresh();
	}

	private void refresh() {
		// 設置標題(民宿名)
		title.setText(favorite_name);

		// 設置副標題(民宿地址)

		address = userCustomDao.getAddressByName(favorite_name);
		subTitle.setText(address.toString());
		
		//設置照片
		pic = userCustomDao.getPicByName(favorite_name);
		byte[] img = pic.getPicture();
		Bitmap bitImg = BitmapFactory.decodeByteArray(img, 0, img.length);
		note_img.setImageBitmap(bitImg);

		// 設置內容(民宿筆記)
		showNotes = userCustomDao.getNotesByName(favorite_name);
		if(showNotes == null)
		{
			inputText.setText("");
			
		}
		else
		{
			inputText.setText(showNotes.toString());
		}
		
		//設置EditText的游標在最後
		int position = inputText.length();
		Editable etext = inputText.getText();
		Selection.setSelection(etext , position);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		menu.add(android.view.Menu.NONE, android.view.Menu.FIRST,
				android.view.Menu.NONE, "確定");
		menu.add(android.view.Menu.NONE, android.view.Menu.FIRST + 1,
				android.view.Menu.NONE, "清除");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case android.view.Menu.FIRST:
			
			//使用者輸入內容 ，更新處存到custom.db的NOTES欄位
			items = inputText.getText().toString();
			// content.setText(items);
			userCustom = new UserCustom(items);
			userCustomDao.update(items, favorite_name);
			break;
		case android.view.Menu.FIRST + 1:
			// 清空所有內容
			inputText.setText("");

		}
		return super.onOptionsItemSelected(item);
	}
}
